package marksys.free.com.marksys.ui.face.facebykomlinplus;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.media.FaceDetector.Face;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import marksys.free.com.marksys.ui.face.facebykomlinplus.callback.FaceDetectionCallBack;
import marksys.free.com.marksys.ui.face.facebykomlinplus.callback.RequsetFaceSearchCallBack;

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {


    private static final String TAG = "CameraSurfaceView";
    Context mContext;
    SurfaceHolder mSurfaceHolder;
    FaceView faceView;
    Handler handler;

    public CameraSurfaceView(Context context) {
        super(context);
        initData(context);
    }

    public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData(context);
    }

    public CameraSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData(context);
    }


    void initData(Context context) {
        mContext = context;
        mSurfaceHolder = getHolder();
        mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);//translucent半透明 transparent透明
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mSurfaceHolder.addCallback(this);
        //FaceHelper mFaceHelper = FaceHelper.getInstance();


    }

    //FaceDetectionListener
    FaceDetectionCallBack faceDetectionCallBack;
    boolean isDetecting = false;


    //PreviewCallback
    private long time;

    private HandlerThread mFaceHandleThread;

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        Log.e(TAG, "onPreviewFrame:");
        if (data != null && data.length > 0 && System.currentTimeMillis() - time > 500) {
            time = System.currentTimeMillis();
            Thread t = new Thread(new FaceThread(data, camera));
            t.start();
        }
    }


    //SurfaceHolder.Callback

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.e(TAG, "surfaceCreated...");
        CameraInterface.getInstance().doOpenCamera(mContext, new CameraInterface.CamOpenOverCallback() {
            @Override
            public void cameraHasOpened() {
                Log.e(TAG, "cameraHasOpened: ");
                CameraInterface.getInstance().getmCamera().setPreviewCallback(CameraSurfaceView.this);
                CameraInterface.getInstance().doStartPreview(mContext, mSurfaceHolder, CameraSurfaceView.this);

            }
        });

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        CameraInterface.getInstance().doStopCamera();
    }

    private class FaceThread implements Runnable {
        private byte[] mData;
        private Camera mtCamera;

        public FaceThread(byte[] data, Camera camera) {
            mData = data;
            mtCamera = camera;
        }

        @Override
        public void run() {
            if (faceView != null) {
                Camera.Size size = mtCamera.getParameters().getPreviewSize();


                Face[] faces = FaceHelper.getInstance().findFaces(mData, size.width, size.height, handler);
                faceView.setFaces(faces);
                dealfaces(faces, mtCamera);
            }
        }
    }

    synchronized void dealfaces(Face[] faces, Camera camera) {

        if (faces == null || faces.length < 1 || faces[0] == null || faces[0].confidence() < 0.51 || isDetecting) {
            return;
        }
        Log.e(TAG, "dealfaces: " + faces[0].confidence());


        isDetecting = true;
        camera.takePicture(null, null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, final Camera camera) {

                if (data != null && faceDetectionCallBack != null) {
                    faceDetectionCallBack.call(data, new RequsetFaceSearchCallBack() {
                        @Override
                        public void call() {
                            CameraSurfaceView.this.isDetecting = false;
                        }
                    });
                }
                camera.startPreview();//开启预览


            }
        });


    }
}
